package com.societyGames.flashForms.basicSkin
{
import com.societyGames.flashForms.DockUnitType;
import com.societyGames.flashForms.DynamicTextField;
import com.societyGames.flashForms.ExpanderContainer;
import com.societyGames.flashForms.ExpanderHeaderContainer;
import com.societyGames.flashForms.FourStateButton;
import com.societyGames.flashForms.FrameContainer;
import com.societyGames.flashForms.FrameOutsideContainer;
import com.societyGames.flashForms.HorizontalSlidingDoorContainer;
import com.societyGames.flashForms.HorizontalSplitContainer;
import com.societyGames.flashForms.ListItem;
import com.societyGames.flashForms.ScrollContainer;
import com.societyGames.flashForms.TabItem;
import com.societyGames.flashForms.TabItemsContainer;
import com.societyGames.flashForms.TextButton;
import com.societyGames.flashForms.TextOverDisplayObject;
import com.societyGames.flashForms.VerticalScrollContainer;
import com.societyGames.flashForms.VerticalSplitContainer;
import com.societyGames.flashForms.Window;

import flash.display.Bitmap;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;

public class BasicControls
{
  public static const BACKGROUND_COLOR:uint = 0xD6E5F5;
  public static const TEXT_BUTTON_HEIGHT:int = 28;

  //This guarantees that the embedded font is loaded.
  [Embed(source="/../assets/InterfaceAssets.swf", symbol="InconsolataFontHolder")]
  private static const _inconsolataFontHolderClass:Class; //DO NOT USE THIS CLASS!

  [Embed(source="/../assets/InterfaceAssets.swf", symbol="ArialFontHolder")]
  private static const _openSansFontHolder:Class; //DO NOT USE THIS CLASS!

  /*//This guarantees that the embedded font is loaded.
  [Embed(source="/../assets/Inconsolata.otf",
          fontName="Inconsolata",
          mimeType="application/x-font",
          fontWeight="normal",
          fontStyle="normal",
    //unicodeRange="Basic Latin",
          advancedAntiAliasing="true",
          embedAsCFF="false")]
  private static const _inconsolataFontHolderClass:Class; //DO NOT USE THIS CLASS!

  [Embed(source="/../assets/OpenSans-Regular.ttf",
          fontName="Arial",
          mimeType="application/x-font",
          fontWeight="normal",
          fontStyle="normal",
    //unicodeRange="Basic Latin",
          advancedAntiAliasing="true",
          embedAsCFF="false")]
  private static const _arialFontHolder:Class; //DO NOT USE THIS CLASS!

  [Embed(source="/../assets/OpenSans-Bold.ttf",
          fontName="Arial",
          mimeType="application/x-font",
          fontWeight="bold",
          fontStyle="normal",
    //unicodeRange="Basic Latin",
          advancedAntiAliasing="true",
          embedAsCFF="false")]
  private static const _arialBoldFontHolder:Class; //DO NOT USE THIS CLASS!

  [Embed(source="/../assets/OpenSans-Italic.ttf",
          fontName="Arial",
          mimeType="application/x-font",
          fontWeight="normal",
          fontStyle="italic",
    //unicodeRange="Basic Latin",
          advancedAntiAliasing="true",
          embedAsCFF="false")]
  private static const _arialItalicFontHolder:Class; //DO NOT USE THIS CLASS! */

  public static function createExpanderContainer():ExpanderContainer
  {
    var expanderContainer:ExpanderContainer = new ExpanderContainer(new BasicAssets.ExpanderCollapsedSpriteClass(),
            new BasicAssets.ExpanderExpandedSpriteClass(),
            new BasicAssets.ExpanderCollapsedOverSpriteClass(),
            new BasicAssets.ExpanderExpandedOverSpriteClass());
    return expanderContainer;
  }

  public static function createExpanderHeaderContainer():ExpanderHeaderContainer
  {
    var expanderHeaderContainer:ExpanderHeaderContainer = new ExpanderHeaderContainer(new BasicAssets.ExpanderCollapsedSpriteClass(),
            new BasicAssets.ExpanderExpandedSpriteClass(),
            new BasicAssets.ExpanderCollapsedOverSpriteClass(),
            new BasicAssets.ExpanderExpandedOverSpriteClass());
    return expanderHeaderContainer;
  }

  public static function createTextButton(clickHandler:Function=null /*(Event):void*/):TextButton
  {
    //Create a generic textField.
    var textField:TextField = SharedTextFields.createTextField();
    textField.autoSize = TextFieldAutoSize.CENTER;
    //Center it.
    //var textFormat:TextFormat = textField.defaultTextFormat;
    //textFormat.align = TextFormatAlign.CENTER;
    //textField.defaultTextFormat = textFormat;
    //Create the button with the textField.
    var textButton:TextButton = new TextButton(textField, new BasicAssets.ButtonClass(), new BasicAssets.SelectedButtonClass(), new BasicAssets.DownButtonClass());
    if (clickHandler != null)
    {
      textButton.buttonClickEventObject.addEventListener(clickHandler);
    }
    textButton.height = TEXT_BUTTON_HEIGHT;
    return textButton;
  }

  public static function createScrollContainer():ScrollContainer
  {
    var horizontalTrack:Sprite = new BasicAssets.HorizontalScrollBarTrackClass();
    var horizontalThumb:Sprite = createHorizontalThumb();

    var verticalTrack:Sprite = new BasicAssets.VerticalScrollBarTrackClass();
    var verticalThumb:Sprite = createVerticalThumb();

    var scrollContainer:ScrollContainer = new ScrollContainer(
            horizontalTrack,
            horizontalThumb,
            verticalTrack,
            verticalThumb
    );
    return scrollContainer;
  }

  private static function createHorizontalThumb():Sprite
  {
    var horizontalThumb:Sprite = new BasicAssets.HorizontalScrollBarThumbClass();
    //The thumb is composed of two pieces: the actual thumb and the grip.
    //Create a self centering and disappearing grip.
    /*var horizontalGrip:DisplayObject = new BasicAssets.HorizontalScrollBarGripClass();
    var centerScaleContainer:CenterScaleContainer = new CenterScaleContainer();
    centerScaleContainer.item = horizontalGrip;
    //Layer the grip on the thumb.
    var itemsContainer:ItemsContainer = new ItemsContainer();
    //Make sure the grip is the same height as the thumb.
    itemsContainer.height = horizontalThumb.height;
    itemsContainer.addItem(horizontalThumb);
    itemsContainer.addItem(centerScaleContainer);*/
    return horizontalThumb;
  }

  public static function createVerticalScrollContainer():VerticalScrollContainer
  {
    var verticalTrack:Sprite = new BasicAssets.VerticalScrollBarTrackClass();
    var verticalThumb:Sprite = createVerticalThumb();
    //Create the actual scroll container
    var verticalScrollContainer:VerticalScrollContainer = new VerticalScrollContainer(
            verticalTrack,
            verticalThumb
    );
    return verticalScrollContainer;
  }

  private static function createVerticalThumb():Sprite
  {
    var verticalThumb:Sprite = new BasicAssets.VerticalScrollBarThumbClass();
    //The thumb is composed of two pieces: the actual thumb and the grip.
    //Create a self centering and disappearing grip.
    /*var verticalGrip:DisplayObject = new BasicAssets.VerticalScrollBarGripClass();
    var centerScaleContainer:CenterScaleContainer = new CenterScaleContainer();
    centerScaleContainer.item = verticalGrip;
    //Layer the grip on the thumb.
    var itemsContainer:ItemsContainer = new ItemsContainer();
    //Make sure the grip is the same width as the thumb.
    itemsContainer.width = verticalThumb.width;
    itemsContainer.addItem(verticalThumb);
    itemsContainer.addItem(centerScaleContainer);*/
    return verticalThumb;
  }

  public static function createTabItem(headerName:String, content:Sprite):TabItem
  {
    var headerButton:FourStateButton = createTabHeader(headerName);
    return new TabItem(headerButton, content);
  }

  private static function createTabHeader(text:String):FourStateButton
  {
    return new FourStateButton(
            createTextOverImage(BasicAssets.TabHeaderSelectedClass, text, 0x000000), //0x6D777E),
            createTextOverImage(BasicAssets.TabHeaderClass, text, 0x999999),
            createTextOverImage(BasicAssets.TabHeaderClass, text, 0x555555),
            createTextOverImage(BasicAssets.TabHeaderClass, text, 0x555555));
  }

  private static function createTextOverImage(imageClass:Class, text:String, textColor:uint):Sprite
  {
    var textField:TextField = SharedTextFields.createSingleLineTextField();
    textField.mouseEnabled = false;
    textField.textColor = textColor;
    textField.text = text;

    var textOverDisplayObject:TextOverDisplayObject = new TextOverDisplayObject(new imageClass() as DisplayObject, textField);
    return textOverDisplayObject;
  }

  public static function createWindow(name:String = null):Window
  {
    var windowSprite:Sprite = new BasicAssets.WindowSpriteClass();
    var window:Window = new Window(windowSprite, 8, 8, 8, 8, 18, Cursors.leftRightCursor, Cursors.upDownCursor, Cursors.leftDownCursor, Cursors.rightDownCursor);
    if (name != null)
    {
      var textField:TextField = SharedTextFields.createTextField();
      textField.selectable = false;
      textField.text = name;
      window.header = textField;
    }
    return window;
  }

  public static function createDebugCircle(radius:Number):Sprite
  {
    var sprite:Sprite = new Sprite();
    sprite.graphics.beginFill(0xFF0000);
    sprite.graphics.drawCircle(radius, radius, radius);
    return sprite;
  }

  public static function createHorizontalSplitContainer():HorizontalSplitContainer
  {
    var splitter:DisplayObject = new BasicAssets.HorizontalSplitterClass();
    var horizontalSplitContainer:HorizontalSplitContainer = new HorizontalSplitContainer(splitter, Cursors.upDownCursor);
    return horizontalSplitContainer;
  }

  public static function createVerticalSplitContainer():VerticalSplitContainer
  {
    var splitter:DisplayObject = new BasicAssets.VerticalSplitterClass();
    var verticalSplitContainer:VerticalSplitContainer = new VerticalSplitContainer(splitter, Cursors.leftRightCursor);
    return verticalSplitContainer;
  }

  public static function createHorizontalSlidingDoorContainer():HorizontalSlidingDoorContainer
  {
    var leftSplitter:DisplayObject = new BasicAssets.VerticalSplitterClass();
    var rightSplitter:DisplayObject = new BasicAssets.VerticalSplitterClass();
    var horizontalSlidingDoorContainer:HorizontalSlidingDoorContainer = new HorizontalSlidingDoorContainer(leftSplitter, rightSplitter, Cursors.leftRightCursor);
    return horizontalSlidingDoorContainer;
  }

  public static function createListItem(text:String, textColor:uint = 0, isBold:Boolean = false):ListItem
  {
    var unselected:FrameContainer = createListItemFrameContainer(text, new Sprite(), textColor, isBold);
    var over:FrameContainer = createListItemFrameContainer(text, new BasicAssets.ListItemOverClass(), textColor, isBold);
    var selected:FrameContainer = createListItemFrameContainer(text, new BasicAssets.ListItemSelectedClass(), textColor, isBold);
    var listItem:ListItem = new ListItem(unselected, selected, over);
    return listItem;
  }

  private static function createListItemFrameContainer(text:String, backGround:Sprite, textColor:uint = 0, isBold:Boolean = false):FrameContainer
  {
    var textField:TextField = SharedTextFields.createTextField();
    textField.textColor = textColor;

    var textFormat:TextFormat = textField.defaultTextFormat;
    textFormat.bold = isBold;
    textField.defaultTextFormat = textFormat;

    textField.height = 19;
    textField.mouseEnabled = false;

    textField.text = text;

    var frameContainer:FrameContainer = new FrameContainer(backGround, 1, 1, 0, 0);
    frameContainer.height = 20;
    frameContainer.item = textField;
    frameContainer.buttonMode = true;
    frameContainer.useHandCursor = true;

    return frameContainer;
  }

  public static function createTabItemsContainer():TabItemsContainer
  {
    var tabItemsContainer:TabItemsContainer = new TabItemsContainer(30, DockUnitType.pixel);
    return tabItemsContainer;
  }

  public static function createToolTipFrameContainer():FrameContainer
  {
    var frameContainer:FrameContainer = new FrameContainer(new BasicAssets.ToolTipClass(), 3, 3, 3, 3);
    return frameContainer;
  }

  public static function createFrameOutsideContainer():FrameOutsideContainer
  {
    return new FrameOutsideContainer
    (
      createBitmap(BasicAssets.FrameTopLeftClass),
      createBitmap(BasicAssets.FrameTopClass),
      createBitmap(BasicAssets.FrameTopRightClass),
      createBitmap(BasicAssets.FrameLeftClass),
      createBitmap(BasicAssets.FrameRightClass),
      createBitmap(BasicAssets.FrameBottomLeftClass),
      createBitmap(BasicAssets.FrameBottomClass),
      createBitmap(BasicAssets.FrameBottomRightClass)
    );
  }

  public static function createBitmap(bitmapDataClass:Class):Bitmap
  {
    var bitmap:Bitmap = new bitmapDataClass() as Bitmap;
    bitmap.smoothing = false;
    return bitmap;
  }

  public static function createInteractiveBitmap(bitmapDataClass:Class):Sprite
  {
    var sprite:Sprite = new Sprite();
    sprite.addChild(createBitmap(bitmapDataClass));
    return sprite;
  }

  private static const INSTRUCTION_STYLE:String =
          "p{font-family:Open Sans;font-size:18;} .code{color:#000000;textIndent:15;font-family:Inconsolata;font-size:18;} .inlineCode{color:#000000;font-family:Inconsolata;font-size:18;} .term{color:#000000;text-decoration:underline;font-style:italic;}";

  public static function createInstructionTextField():TextField
  {
    var textField:TextField = new DynamicTextField();
    SharedTextFields.setDefaultRender(textField);
    textField.autoSize = TextFieldAutoSize.CENTER; //Fixed weird bug where links jitter and text kerns badly.
    textField.multiline = true;
    textField.wordWrap = true;
    textField.condenseWhite = true;
    SharedTextFields.addStyle(textField, SharedTextFields.LINK_STYLE);
    SharedTextFields.addStyle(textField, INSTRUCTION_STYLE);
    return textField;
  }
}
}